package com.memoir.service.impl.common;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.memoir.common.constant.RedisConstants;
import com.memoir.model.entity.image.MPageLableEntity;
import com.memoir.model.entity.image.MSpaceTagEntity;
import com.memoir.model.entity.image.MSpaceTypeEntity;
import com.memoir.model.vo.rep.common.QueryPageLableRespVo;
import com.memoir.model.vo.rep.common.QuerySpaceTypeListRespVo;
import com.memoir.model.vo.rep.common.QueryTagListRespVo;
import com.memoir.service.common.ImageCommonService;
import com.memoir.util.RedisUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ImageCommonServiceImpl implements ImageCommonService {

    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<QueryTagListRespVo> queryTagList() {
        Object dataType = redisUtil.get(RedisConstants.SPACE_TYPE_LITS);
        List<QueryTagListRespVo> returnDataList = new ArrayList<>();
        if(null != dataType){
            JSONArray dataJSONArray = JSONArray.parseArray(dataType.toString());
            if (null != dataJSONArray){
                List<MSpaceTagEntity> tagEntityList = dataJSONArray.toJavaList(MSpaceTagEntity.class);
                if(CollectionUtil.isNotEmpty(tagEntityList)){
                    for(MSpaceTagEntity entity : tagEntityList){
                        QueryTagListRespVo data = new QueryTagListRespVo();
                        BeanUtils.copyProperties(entity,data);
                        returnDataList.add(data);
                    }
                }
            }
        }
        return returnDataList;
    }

    @Override
    public List<QuerySpaceTypeListRespVo> querySpaceTypeList() {
        Object dataType = redisUtil.get(RedisConstants.SPACE_TAG_LITS);
        List<QuerySpaceTypeListRespVo> returnDataList = new ArrayList<>();
        if(null != dataType){
            JSONArray dataJSONArray = JSONArray.parseArray(dataType.toString());
            if (null != dataJSONArray){
                List<MSpaceTypeEntity> tagEntityList = dataJSONArray.toJavaList(MSpaceTypeEntity.class);
                if (CollectionUtil.isNotEmpty(tagEntityList)){
                    for(MSpaceTypeEntity  entity : tagEntityList){
                        QuerySpaceTypeListRespVo data = new QuerySpaceTypeListRespVo();
                        BeanUtils.copyProperties(entity,data);
                        returnDataList.add(data);
                    }
                }
            }
        }
        return returnDataList;
    }

    @Override
    public List<QueryPageLableRespVo> queryPageLable() {
        Object dataType = redisUtil.get(RedisConstants.SPACE_TAG_LITS);
        List<QueryPageLableRespVo> returnDataList = new ArrayList<>();
        if(null != dataType){
            JSONArray dataJSONArray = JSONArray.parseArray(dataType.toString());
            if (null != dataJSONArray){
                List<MPageLableEntity> lableEntities = dataJSONArray.toJavaList(MPageLableEntity.class);
                if(CollectionUtil.isNotEmpty(lableEntities)){
                    for(MPageLableEntity  entity : lableEntities){
                        QueryPageLableRespVo data = new QueryPageLableRespVo();
                        BeanUtils.copyProperties(entity,data);
                        returnDataList.add(data);
                    }
                }
            }
        }
        return returnDataList;
    }
}
